package com.fingard.dsp.bank.directbank.payfree01;

import java.util.Enumeration;
import java.util.Hashtable;

import com.ebs.epos.gw.client.data.PayData;
import com.ebs.epos.gw.client.trans.EposPay;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;


public class PAYFREE01RealTime extends PAYFREE01Base implements IRealTime {
	
	//单笔，不需采用流处理方式
	public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception{
		//返回ATS的对象
		RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		
		
		try {
			StringBuilder sbLog = new StringBuilder();
			ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "realtimeRec");
			if(actItem == null){
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = "未找到账号"+p_req9108.singleDetail.recAct+"的实时商户配置节点";
				tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
				tmpRetDetail.transState = PaymentState.FAIL_Payment;
				tmpRetDetail.payInfoCode = "E9981";
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
			PayData payData = new PayData();

			payData.setTransType("611");
			sbLog.append("\r\nsetTransType:611");
			
			payData.setMerchantId(actItem.merchantId);
			sbLog.append("\r\nsetMerchantId:"+actItem.merchantId);
			
			payData.setInMerchantId(actItem.inMerchantId);
			sbLog.append("\r\nsetInMerchantId:"+actItem.inMerchantId);
			
			payData.setTermId(actItem.termId);
			sbLog.append("\r\nsetTermId:"+actItem.termId);
			
			payData.setOrderNo(p_req9108.singleDetail.askNumber);
			sbLog.append("\r\nsetOrderNo:"+p_req9108.singleDetail.askNumber);
			
			payData.setMcTransDateTime(Format.Date8Format.format(p_req9108.singleDetail.getPayDateTime()));
			sbLog.append("\r\nsetMcTransDateTime:"+Format.Date8Format.format(p_req9108.singleDetail.getPayDateTime()));
			
			payData.setMcSequenceNo(p_req9108.singleDetail.askNumber);
			sbLog.append("\r\nsetMcSequenceNo:"+p_req9108.singleDetail.askNumber);
			
			payData.setAmount(p_req9108.singleDetail.payAmount);
			sbLog.append("\r\nsetAmount:"+p_req9108.singleDetail.payAmount);
			
			payData.setCardNo(p_req9108.singleDetail.payAct);
			sbLog.append("\r\nsetCardNo:"+p_req9108.singleDetail.payAct);
			
			payData.setAccName(p_req9108.singleDetail.payName);
			sbLog.append("\r\nsetAccName:"+p_req9108.singleDetail.payName);
			
			payData.setIdInfo(p_req9108.singleDetail.certNum);
			sbLog.append("\r\nsetIdInfo:"+p_req9108.singleDetail.certNum);
			//payData.setEposHeader("");
			payData.setRemark(p_req9108.singleDetail.purpose);
			sbLog.append("\r\nsetRemark:"+p_req9108.singleDetail.purpose);
			if(p_req9108.singleDetail.cardType.equals("1")){
				payData.setAccType("01");
				sbLog.append("\r\nsetAccType:01");
			}
			else if(p_req9108.singleDetail.cardType.equals("3")){
				payData.setAccType("02");//00借记卡 01存折 02 信用卡			默认设置为：“00”-借记卡
				sbLog.append("\r\nsetAccType:02");
			}
			else{
				payData.setAccType("00");
				sbLog.append("\r\nsetAccType:00");
			}
			payData.setBankCity(p_req9108.singleDetail.getOppProvinceAndCity()[1]);
			sbLog.append("\r\nsetBankCity:"+p_req9108.singleDetail.getOppProvinceAndCity()[1]);
			//payData.setBankCode(getBankConfig().getMapValueByKey("bankMapping", p_req9108.singleDetail.payBankCode));
			//payData.setESI("");
			this.WriteBankLogStep2(sbLog.toString());
			PayData retPayData = EposPay.DoTrans(payData);
			if (retPayData == null) {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "EposPay.DoTrans error:retPayData is null.";
				tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
				//System.out.println("EposPay.DoTrans error.");
				this.WriteBankLogStep3(tmpRetDetail.respInfo);
			} else {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = retPayData.getPrivateData();//retPayData.getMsgExt();
				tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
				tmpRetDetail.transState = retPayData.getRespCode();
				tmpRetDetail.payInfoCode = retPayData.getRespCode();
				
				if(this.getBankConfig().getValueAsBool("Set9108FailIfNot0000") == true){
					String bkRespCode = StringHelper.trimEnd(tmpRetDetail.transState, "0");
					if(!tmpRetDetail.transState.equals("0000") && bkRespCode.length() > 0){
						tmpRetDetail.transState = PaymentState.FAIL_Payment;
					}
				}
				
				sbLog = new StringBuilder();
				try{
					//System.out.print(retPayData);
					sbLog.append("\r\ngetRespCode:"+retPayData.getRespCode());
					sbLog.append("\r\ngetPrivateData:"+retPayData.getPrivateData());
					sbLog.append("\r\ngetReturnType:"+retPayData.getReturnType());
					sbLog.append("\r\ngetOriRespCode:"+retPayData.getOriRespCode());
					sbLog.append("\r\ngetDatas:");
					@SuppressWarnings("rawtypes")
					Hashtable tmpHash = retPayData.getDatas();
					@SuppressWarnings("unchecked")
					Enumeration<Object> en1 = tmpHash.keys();
					while(en1.hasMoreElements()) {
						Object eachKey = en1.nextElement();
						sbLog.append("\r\n"+eachKey+":"+tmpHash.get(eachKey));
					}
				}
				catch(Exception ex){
					sbLog.append(ex.getMessage()+"\r\n");
				}
				this.WriteBankLogStep3(sbLog.toString());
				
			}

		} catch (Exception ex) {
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
			//this.WriteBankLogLn(ex.getMessage());
			this.WriteBankLogLn(ex.getMessage());
			ex.printStackTrace();
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	//9144 状态查询
	public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception{
		//返回ATS的对象
		RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
		
		
		try {
			StringBuilder sbLog = new StringBuilder();
			ActSetItem actItem = getBankConfig().getActSetItem(p_req9144.recAct, "realtimeRec");
			if(actItem == null){
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = "未找到账号"+p_req9144.recAct+"的实时商户配置节点";
				retAts.writeRetDetail(tmpRetAtsDetail);
				return retAts;
			}
			
			PayData payData = new PayData();

			payData.setTransType("063");
			sbLog.append("\r\nsetTransType:063");
			
			payData.setMerchantId(actItem.merchantId);
			sbLog.append("\r\nsetMerchantId:"+actItem.merchantId);
			
			payData.setInMerchantId(actItem.inMerchantId);
			sbLog.append("\r\nsetInMerchantId:"+actItem.inMerchantId);
			
			payData.setTermId(actItem.termId);
			sbLog.append("\r\nsetTermId:"+actItem.termId);
			
			payData.setOrderNo(p_req9144.askNumber);
			sbLog.append("\r\nsetOrderNo:"+p_req9144.askNumber);
			//payData.setMcTransDateTime(Format.Date8Format.format(new Date()));
			//payData.setMcSequenceNo(p_req9144.reqHeader.transSeqID);
			//payData.setAmount(p_req9144.payAmount);
			//payData.setRemark("");
			this.WriteBankLogStep2(sbLog.toString());
			PayData retPayData = EposPay.DoTrans(payData);
			if (retPayData == null) {
				//System.out.println("EposPay.DoTrans error.");
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = "EposPay.DoTrans error:retPayData is null.";
				this.WriteBankLogStep3(tmpRetAtsDetail.respInfo);
			} else {
				//System.out.print(retPayData);
				if(retPayData.getRespCode().equals("0000")){
					tmpRetAtsDetail.respCode = "0";
					tmpRetAtsDetail.respInfo = retPayData.getMsgExt();
					tmpRetAtsDetail.reqSeqID = p_req9144.askNumber;
					tmpRetAtsDetail.transState = retPayData.getOriTransStatus();
					tmpRetAtsDetail.payInfoCode = retPayData.getOriTransStatus();
				}
				else{
					tmpRetAtsDetail.respCode = "-1";
					tmpRetAtsDetail.respInfo = retPayData.getMsgExt();
				}
				sbLog = new StringBuilder();
				try{
					//System.out.print(retPayData);
					sbLog.append("\r\ngetRespCode:"+retPayData.getRespCode());
					sbLog.append("\r\ngetPrivateData:"+retPayData.getPrivateData());
					sbLog.append("\r\ngetMsgExt:"+retPayData.getMsgExt());
					sbLog.append("\r\ngetOriRespCode:"+retPayData.getOriRespCode());
					sbLog.append("\r\ngetOriTransStatus:"+retPayData.getOriTransStatus());
					sbLog.append("\r\ngetDatas:");
					@SuppressWarnings("rawtypes")
					Hashtable tmpHash = retPayData.getDatas();
					@SuppressWarnings("unchecked")
					Enumeration<Object> en1 = tmpHash.keys();
					while(en1.hasMoreElements()) {
						Object eachKey = en1.nextElement();
						sbLog.append("\r\n"+eachKey+":"+tmpHash.get(eachKey));
					}
				}
				catch(Exception ex){
					sbLog.append(ex.getMessage()+"\r\n");
				}
				this.WriteBankLogStep3(sbLog.toString());
				
			}
			

		} catch (Exception ex) {
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = ex.getMessage();
			this.WriteBankLogLn(ex.getMessage());
			ex.printStackTrace();
		}
		
		
		
		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

}
